﻿using System;
using System.Reflection;
using log4net.Config;
using Loki.Engine;
using Loki.Utils;
using Luna.Data;
using Luna.Data.Budget;
using Luna.Data.Configuration;
using Luna.Data.CRM;
using Luna.Services.Test;
using Luna.Services.Test.Budget;
using Luna.Services.Test.Contacts;
using NUnit.Framework;
using Rhino.Mocks;

[SetUpFixture]
public class TestSetup
{
    private TestApplicationModel _Model = new TestApplicationModel();

    [SetUp]
    public void Initialize()
    {
        var L_Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Luna.Services.Test.log4net.config");
        XmlConfigurator.Configure(L_Stream);
        L_Stream.Dispose();

        // Initialize Data engine
        try
        {
            ToolKit.Engine.DefineApplicationModel(_Model);
            _Model.Initialize();

            IConfigurationProvider ConfigurationData = MockRepository.GenerateMock<IConfigurationProvider>();
            IContactProvider ContactData = MockRepository.GenerateMock<IContactProvider>();
            IBudgetProvider BudgetData = MockRepository.GenerateMock<IBudgetProvider>();
            IDataContext DataContext = MockRepository.GenerateMock<ObjectModel, IDataContext>() as IDataContext;

            DataContext.Stub(x => x.Configuration).Return(ConfigurationData);
            DataContext.Stub(x => x.Contacts).Return(ContactData);
            DataContext.Stub(x => x.Budget).Return(BudgetData);

            ContactDataCreator ContactDataManager = new ContactDataCreator();
            ContactDataManager.Setup(DataContext);

            BudgetDataCreator BudgetDataManager = new BudgetDataCreator();
            BudgetDataManager.Setup(DataContext);

            DataCreator Creator = new DataCreator();
            Creator.SetUp(DataContext);

            ToolKit.Engine.DefineDataModel(DataContext as ObjectModel);
        }
        catch (Exception ex)
        {
            Console.Out.WriteLine(ex.Message);
        }
    }

    [TearDown]
    public void Clean()
    {
        ToolKit.Reset();
    }
}